package com.ccys.doctor.controller;

import cn.hutool.core.date.DateUtil;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.doctor.pendToConsult.CreateOfflineBookingDto;
import com.ccys.db.dto.doctor.pendToConsult.GetDoctorTBTCAppointmentListDto;
import com.ccys.db.entity.TblDoctor;
import com.ccys.db.mapper.TblBookingConsultationMapper;
import com.ccys.db.mapper.TblBookingQueueMapper;
import com.ccys.db.mapper.TblDoctorMapper;
import com.ccys.db.page.AppPageDto;
import com.ccys.db.vo.doctor.pendingToConsult.*;
import com.ccys.doctor.service.ConsultStageService;
import com.ccys.doctor.service.PendingToConsultService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author：XLY
 * @Date：2024/3/2 0002
 */
@RestController
@RequestMapping("/pendingToConsult")
@Api(tags = "Pending To Consult")
@Slf4j
public class PendingToConsultController {




    @Resource
    private TblDoctorMapper doctorMapper;


    @Resource
    private TblBookingConsultationMapper bookingConsultationMapper;



    @Resource
    private ConsultStageService consultStageService;


    @Resource
    private TblBookingQueueMapper bookingQueueMapper;


    @Resource
    private PendingToConsultService pendingToConsultService;



    @ApiOperation("Pending To Consult/ 124")
    @PostMapping("CreateOfflineBooking")
    public ResponseResultApp createOfflineBooking(@RequestBody CreateOfflineBookingDto dto){
        pendingToConsultService.createOfflineBooking(dto);
        return ResponseResultApp.success();
    }


    @ApiOperation("Pending To Consult/ 125")
    @GetMapping("GetCheckPassConsultants")
    public ResponseResultApp getCheckPassConsultants(String member_id,String child_id){
        member_id = StringUtils.isNotEmpty(child_id)?child_id:member_id;
        String doctorId = UserUtil.getCurLoginUserId();
        bookingConsultationMapper.getCheckPassConsultants(doctorId,member_id);
        return ResponseResultApp.success();
    }


    @ApiOperation("Pending To Consult/ 126")
    @GetMapping("GetConsultantBeginDate")
    public ResponseResultApp<GetConsultantBeginDateVo> getConsultantBeginDate(String bookingId){
        Date endDate = bookingConsultationMapper.getConsultantBeginDate(bookingId);
        GetConsultantBeginDateVo vo = new GetConsultantBeginDateVo();
        long days = DateUtil.betweenDay(endDate, new Date(), true);
        if (days >= 0 && days <= 2) {
            vo.setWithin_1_Day(days == 0);
            vo.setWithin_2_Days(days == 1);
            vo.setWithin_3_Days(days == 2);
        } else {
            vo.setWithin_1_Day(false);
            vo.setWithin_2_Days(false);
            vo.setWithin_3_Days(false);
        }
        return ResponseResultApp.success(vo);
    }


    @ApiOperation("Pending To Consult/ 127")
    @GetMapping("GetDoctorCountryTimeZone")
    public ResponseResultApp getDoctorCountryTimeZone(){
        String doctorUserId = UserUtil.getCurLoginUserId();
        Date date = consultStageService.getDoctorCountryTimeZone(doctorUserId);
        return ResponseResultApp.success(date);
    }


    @ApiOperation("Pending To Consult/ 128")
    @GetMapping("GetDoctorTBTCAppointmentList")
    public ResponseResultApp<List<GetDoctorTBTCAppointmentListVo>> getDoctorTBTCAppointmentList(){
        String doctorId = UserUtil.getCurLoginUserId();
        GetDoctorTBTCAppointmentListDto dto = new GetDoctorTBTCAppointmentListDto();
        dto.setDoctorId(doctorId);
        dto.setType(1);
        List<GetDoctorTBTCAppointmentListVo> res = bookingConsultationMapper.getDoctorTBTCAppointmentList(dto);
        return ResponseResultApp.success(res);
    }



    @ApiOperation("Pending To Consult/ 129")
    @GetMapping("GetIncomingPatient")
    public ResponseResultApp<GetIncomingPatientVo> getIncomingPatient(){
        //照片路径
        String doctorId = UserUtil.getCurLoginUserId();
        GetIncomingPatientVo res = bookingConsultationMapper.getIncomingPatient(doctorId);
        return ResponseResultApp.success(res);
    }



    @ApiOperation("Pending To Consult/ 131")
    @GetMapping("getNumOfPatientOnQueue")
    public ResponseResultApp<GetNumOfPatientOnQueueVo> getNumOfPatientOnQueue(){

        String doctorId = UserUtil.getCurLoginUserId();
        TblDoctor doctor = doctorMapper.selectByPrimaryKey(doctorId);
        Integer panelType = doctor.getPanelType();
        GetNumOfPatientOnQueueVo count = bookingQueueMapper.getNumOfPatientOnQueue(doctorId, panelType);
        //查询这个医生是专科还是全科，sp查询自己的，gp查询所有在队列里面是属于全科医生的
        return ResponseResultApp.success(count);
    }

    @ApiOperation("Pending To Consult/ 132")
    @GetMapping("GetNumOfPatientOnQueue_NotSelectedDoctor")
    public ResponseResultApp<GetNumOfPatientOnQueueVo>  getNumOfPatientOnQueueNotSelectedDoctor(){
        //如果是sp，那么直接返回0
        String doctorId = UserUtil.getCurLoginUserId();
        GetNumOfPatientOnQueueVo vo = new GetNumOfPatientOnQueueVo();
        TblDoctor doctor = doctorMapper.selectByPrimaryKey(doctorId);
        Integer panelType = doctor.getPanelType();
        if(Objects.nonNull(panelType)&&panelType==1){
            vo.setCount(0);
        }else{
            vo = bookingQueueMapper.getNumOfPatientOnQueueNoSelectedDoctor(doctorId);
        }
        //如果是gp，查询status in(0,1,2,3) And cousult_doctor_id is null
        return ResponseResultApp.success(vo);
    }


    @ApiOperation("Pending To Consult/ 133")
    @GetMapping("GetPatientUpcomingAppointmentList")
    public ResponseResultApp<List<GetDoctorTBTCAppointmentListVo>> getPatientUpcomingAppointmentList(String mem_id,String child_id){
        String doctorId = UserUtil.getCurLoginUserId();
        GetDoctorTBTCAppointmentListDto dto = new GetDoctorTBTCAppointmentListDto();
        dto.setDoctorId(doctorId);
        dto.setType(2);
        Date date = consultStageService.getDoctorCountryTimeZone(doctorId);
        dto.setEndTime(date);
        mem_id = StringUtils.isNotEmpty(child_id)?child_id:mem_id;
        dto.setMemberId(mem_id);
        List<GetDoctorTBTCAppointmentListVo> res = bookingConsultationMapper.getDoctorTBTCAppointmentList(dto);
        return ResponseResultApp.success(res);
    }



    @ApiOperation("Pending To Consult/ 134")
    @GetMapping("GetTBTCIncomingPatient")
    public ResponseResultApp getTBTCIncomingPatient(){
        return consultStageService.getTBTCConsultingOrIncomingPatient("","incoming");
    }



    @ApiOperation("Pending To Consult/ 135")
    @GetMapping("GetWaitingForApprovingConsultations")
    public ResponseResultApp<GetWaitingForApprovingConsultationsVo> getWaitingForApprovingConsultations(String patient_country_of_residence, String doctor_country_of_residence){
        AppPageDto.startPage();
        List<GetWaitingForApprovingConsultationsVo.BookingVo> bookings = bookingConsultationMapper.getWaitingForApprovingConsultations(patient_country_of_residence,doctor_country_of_residence);
        PageInfo<GetWaitingForApprovingConsultationsVo.BookingVo> info = new PageInfo<>(bookings);
        GetWaitingForApprovingConsultationsVo vo = new GetWaitingForApprovingConsultationsVo();
        vo.setTotalItems(info.getPageSize());
        vo.setBookings(bookings);
        return ResponseResultApp.success(vo);
    }





}
